(function() {
	var remote_server_url = "http://121.40.18.23:1518/api/complex/";
	// var remote_server_url = "http://localhost:1518/api/complex/";

	var window_height = window.screen.height;
	$("#store_map").height(window_height);

	var map; // 绑定map全局对象
	var localCity; // 绑定全局定位城市

	//百度地图
	var ak_key = "rroTv2TmcdKGCiQ2jER1oA3V";
	var doc_write = document.write;
	var doc_s_write = function(HTML) {
		var dom = $(HTML);
		console.log("heml" + HTML);
		dom.find("script[src]").each(function(i, scriptNode) {
			scriptNode.src && require([scriptNode.src]);
			scriptNode.removeAttribute("src")
		});
		dom.appendTo(document.body);
	};
	document.write = doc_s_write;
	require(["http://api.map.baidu.com/api?v=2.0&ak=Noga0lSLMrk62roymqXkgaUB"], function(scriptText) {
		document.write = doc_write;
		var _BMap_ti = setInterval(function() {
			if (window.BMap) {
				var store_map_dtd = new $.Deferred();
				$.when(init_map(store_map_dtd)).then(function(point) {
					map.centerAndZoom(point, 11);
					var gc = new BMap.Geocoder();
					gc.getLocation(point, function(as) {
						var ac = as.addressComponents;
						localCity = ac.city;
						// 根据本地城市名称搜索lbs云数据
						$.getJSON(remote_server_url + "ClientStoreMapFactory", {
							city: localCity.name || '深圳市'
						}, function(result) {
							console.log(result);
							map_add_point(result.contents);
						});
					});
				});
				clearInterval(_BMap_ti);
			};
		});
	});

	var init_map = function(dtd) { // 获取地图定位点
		// 百度地图API功能
		map = new BMap.Map("store_map"); // 创建Map实例
		map.addControl(new BMap.NavigationControl({
			anchor: BMAP_ANCHOR_BOTTOM_LEFT,
			type: BMAP_NAVIGATION_CONTROL_SMALL
		})); //添加地图缩放控件	
		// map.addControl(new BMap.ScaleControl()); // 添加比例尺控件
		var point;
		if (App.get("$Cache.top.coordinate.latitude") || App.get("$Cache.main.location_city")) {
			point = new BMap.Point(App.get("$Cache.top.coordinate.longitude"), App.get("$Cache.top.coordinate.latitude"));
			dtd.resolve(point);
		} else {
			var Geolocation = window.navigator.geolocation;
			Geolocation.getCurrentPosition(function(r) {
				if (this.getStatus() == BMAP_STATUS_SUCCESS) {
					point = new BMap.Point(r.coords.longitude, r.coords.latitude);
					dtd.resolve(point);
				};
			});
		};
		return dtd;
	};

	// 为地图添加带有信息框的标注点
	var map_add_point = function(marker_points) {
		marker_points.forEach(function(point, index) {
			// 创建标注点
			var marker = new BMap.Marker(new BMap.Point(point.location[0], point.location[1]));
			// 设置信息框内容
			var sContent = "<a href='http://" + document.domain + "/store_preview.html?geotable_id=" + point.uid + "'>" +
				"<span style='font-size:16;font-weight:bold;padding-right:15px'>" + point.title + "</span>" +
				"<span style='color:blue'>详情...</span>" +
				"<div><img class='store_img' src='" + point.store_imgs.split(",")[0] + "?imageView/1/w/200' /></div>" +
				"</a>" +
				"<p>地址：" + point.address + "</p>" +
				"<p>电话：<a href='tel:" + (point.phone || point.mobile) + "'>" + (point.phone || point.mobile) + "</a></p>"
			map.addOverlay(marker);
			marker.addEventListener("click", function() {
				var info_window = marker_open_info_window(sContent);
				this.openInfoWindow(info_window);
			});
		});
	};
	// 标注点打开信息框事件
	var marker_open_info_window = function(content) {
		// 创建信息框对象并返回
		var infoWindow = new BMap.InfoWindow(content, {
			width: 200
		});
		infoWindow.enableAutoPan();
		// 信息框打开时重绘一次，确保内容煤油溢出或挤压
		infoWindow.addEventListener("open", function() {
			document.getElementsByClassName("store_img")[0].onload = function() {
				infoWindow.redraw();
			};
		});
		return infoWindow;
	};


}());